angular.module("Horizon").controller('selectLocationCtr',["$scope","Location_ser","$ionicPopup","$interval",function($scope,Location_ser,$ionicPopup,$interval){
	var oCopy=document.querySelectorAll("#copyInfo")[0];
	oCopy.style.display="none";
	$scope.flag=false;
	var date=new Date();
	var year=date.getFullYear();
	var month=date.getMonth()+1;
	var day=date.getDate();
	if(month<10){
		month="0"+month;
	}
	if(day<10){
		day="0"+day;
	}
	var today=year+"-"+month+"-"+day;//2017-04-21;
	var api_url=location.href.split("/");
	var	API_URL=api_url[0]+"//"+api_url[2];
	API_URL="https://ws.abb.com.cn";//测试中使用，真实中删除；
	var timerInit=null;
	var selectAreatimer=null;
	var selectDateTimer=null;
	clearInterval(timerInit);
	clearInterval(selectAreatimer);
	clearInterval(selectDateTimer);
	if(location.search){
		hash(API_URL);
	}
	var oMain=document.querySelectorAll(".meetRoom")[0];
	$scope.time=date;

    var timer = $interval(function(){
        $scope.time = new Date();
    },60000);

	$scope.timeStatus=true;
	$scope.selectTimeStatus=false;
	$scope.loadShow=false;
	var oCountry=document.getElementById("country");
	var oCity=document.getElementById("city");
	var oBodyHeight=document.body.clientHeight;
	var oFoot=document.querySelectorAll(".foot")[0];
	oFoot.style.position="absolute";
	oFoot.style.top=oBodyHeight-30+"px";
	Location_ser.getData({
		url: API_URL+"/mScanningTV/Home/GetAllMeetingRoomLocation",
		successCallback:function(data){
			$scope.LocationData=data;
			$scope.selectLocation(data);
		}
	});
	$scope.selectLocation=function(area){
		$scope.data=area;
	}
	$scope.proToCity=function(){
		$scope.cities=$scope.pro.Location;
	}
	$scope.cityToArea=function(){
		$scope.loadShow=true;
		var url=API_URL+"/mScanningTV/Home/GetMeetingRoomsByLocation";
		var newUrl=url+"?country="+$scope.pro.Country+"&location="+$scope.city;
		var oCountry=document.getElementById("country");
		var oCity=document.getElementById("city");
		if($scope.pro.Country!=null && $scope.city!=null){
			location.search="?country="+$scope.pro.Country+"&location="+$scope.city;
			oCountry.setAttribute("disabled","disabled");
			oCity.setAttribute("disabled","disabled");
			$scope.Wait=true;
			$scope.flag=false;
			clearInterval(timerInit);
			clearInterval(selectAreatimer);
			clearInterval(selectDateTimer);
			selectAreatimer=setInterval(function(){
				oCountry.setAttribute("disabled","disabled");
				oCity.setAttribute("disabled","disabled");
				getRoomInfo($scope.pro.Country,$scope.city,today);
			},60000);
		}
		getRoomInfo($scope.pro.Country,$scope.city,today);
	}
	$scope.selectDay=function(selectDate,$event,week){
		clearInterval(selectDateTimer);
		clearInterval(timerInit);
		clearInterval(selectAreatimer);
		var oCountry=document.getElementById("country");
		var oCity=document.getElementById("city");
		var oPopup=document.getElementById("popup");
		oPopup.style.display="none";
		oCountry.setAttribute("disabled","disabled");
		oCity.setAttribute("disabled","disabled");
		if(week=="today"){
			$scope.timeStatus=true;
			$scope.selectTimeStatus=false;
		}else{
			$scope.timeStatus=false;
			$scope.selectTimeStatus=true;
			var time=selectDate.split("-");//2017 04 25
			$scope.selectTime=time[2]+"/"+time[1]+"/"+time[0];
			$scope.selectWeek=week;
		}
		var oItemBox=document.querySelectorAll(".itemBox");
		for(var i=0;i<oItemBox.length;i++){
			var eleArrOther=[];
			for(var j=0;j<oItemBox[i].childNodes.length;j++){
				if(oItemBox[i]["childNodes"][j].nodeType==1){
					eleArrOther.push(oItemBox[i]["childNodes"][j]);
				}
			}
			oItemBox[i].setAttribute("disabled","disabled");
			oItemBox[i].style.background="#111";
			eleArrOther[0].style.color="#F2F2F2";
			eleArrOther[1].style.color="#C2CCD1";
		}
		var eleArrThis=[];
		if($event.target.parentNode.className=="itemBox" || $event.target.nodeName=="P"){//非IE；
			$event.target.parentNode.style.background="#fff";
			for(var j=0;j<$event.target.parentNode.childNodes.length;j++){
				if($event.target.parentNode.childNodes[j].nodeType==1){
					eleArrThis.push($event.target.parentNode.childNodes[j]);
				}
			}
			eleArrThis[0].style.color="#000";
			eleArrThis[1].style.color="#333";
		}else{//IE；
			$event.target.style.background="#fff";
			for(var j=0;j<$event.target.childNodes.length;j++){
				if($event.target.childNodes[j].nodeType==1){
					eleArrThis.push($event.target.childNodes[j]);
				}
			}
			eleArrThis[0].style.color="#000";
			eleArrThis[1].style.color="#333";
		}
		var oLoading=document.getElementById("loading");
		selectDateTimer=setInterval(function(){
			oLoading.style.display="block";
			getRoomInfo($scope.pro.Country,$scope.city,selectDate);
		},60000);
		oLoading.style.display="block";
		getRoomInfo($scope.pro.Country,$scope.city,selectDate);
	}
	
	function getRoomInfo(country,city,date){
		var timezone=clientTimeZone();
		Location_ser.getData({
url:API_URL+"/mScanningTV/Home/GetMeetingRoomsByLocation?location="+city+"&timezone="+timezone+"&date="+date,
			successCallback:function(data,status){
				if(status==200){
					if(data){
						$scope.flag=true;
						var oLoading=document.getElementById("loading");
						oLoading.style.display="none";
						$scope.show=true;
						flag=true;
						bookRoomInfo(data,oMain);
						var len=data.length;
						var date=new Date();
						changeRuler(date.getHours(),date.getMinutes(),len);
						var oCountry=document.getElementById("country");
						var oCity=document.getElementById("city");
						oCountry.removeAttribute("disabled","disabled");
						oCity.removeAttribute("disabled","disabled");
						for(var i=0;i<oCountry.options.length;i++){
							if(oCountry.options[i].innerHTML==country){
								oCountry.options[i].setAttribute("selected","true");
								for(var j=0;j<$scope.data.length;j++){
									if($scope.data[j]["Country"]==country){
										$scope.cities=$scope.data[j]["Location"];
										$scope.city=city;
										$scope.pro=$scope.data[j];
										if(city){
											if(city.replace(/%20/gi," ")){
												var cityCode=city.replace(/%20/gi," ");
											}
										}
										setTimeout(function(){
											for(var k=0;k<oCity.options.length;k++){
												if(oCity.options[k].innerHTML==cityCode){
													oCity.options[k].setAttribute("selected","true");
												}
											}
										},50);
									}
								}
							}
						}
						var oItemBox=document.querySelectorAll(".itemBox");
						for(var i=0;i<oItemBox.length;i++){
							oItemBox[i].removeAttribute("disabled","disabled");
						}
					}else{
						$scope.flag=true;
						var oLoading=document.getElementById("loading");
						oLoading.style.display="none";
						$scope.show=true;
						flag=true;
						var oCountry=document.getElementById("country");
						var oCity=document.getElementById("city");
						oCountry.removeAttribute("disabled","disabled");
						oCity.removeAttribute("disabled","disabled");
						var oItemBox=document.querySelectorAll(".itemBox");
						for(var i=0;i<oItemBox.length;i++){
							oItemBox[i].removeAttribute("disabled","disabled");
						}
						var oBooked=document.querySelectorAll(".booked");
						for(var j=0;j<oBooked.length;j++){
							oBooked[j].parentNode.removeChild(oBooked[j]);
						}
					}
				}
			}
        });
	}
	var popup2=null;
	var zIndexNum=100;
	function bookRoomInfo(data,oMain){
		data.sort(keysrt('RoomName',false));
		var num=0;
		if($scope.flag==true){
			$scope.roomData=data;
		}
		var oBooked=document.querySelectorAll(".booked");
		if(oBooked){
			for(var i=0,len=oBooked.length;i<len;i++){
				oMain.removeChild(oBooked[i]);
			}
		}
		for(var i=0,len1=data.length;i<len1;i++){
			if(data[i]["BookList"]){
				for(var j=0,len2=data[i]["BookList"].length;j<len2;j++){
//						if(data[i]["BookList"][j]["Start"].split(" ")[0])<=parseInt(data[i]["BookList"][j]["End"])){
					if(data[i]["BookList"][j]["Start"].split(" ")[1]<=data[i]["BookList"][j]["End"].split(" ")[1]){
						num++;
						var oDiv=document.createElement("div");
						var oSpan=document.createElement("span");
						oDiv.setAttribute("class","booked");
						oDiv.setAttribute("start",data[i]["BookList"][j]["Start"]);
						oDiv.setAttribute("end",data[i]["BookList"][j]["End"]);
						oDiv.setAttribute("roomid",data[i]["RoomName"]);
						oDiv.setAttribute("owner",data[i]["BookList"][j]["BookOwner"]);
						oDiv.setAttribute("tele",data[i]["BookList"][j]["Telephone"]);
						var oCol=document.querySelectorAll(".title .col")[1];
						var oRoomId=document.querySelectorAll(".title .col")[0];
						var colWidth=Math.round(oCol.clientWidth);
						var colHeight=Math.round(oCol.clientHeight);
						var start_time=data[i]["BookList"][j]["Start"].split(" ")[1];
						var start_min=parseInt(start_time.split(":")[1]);
						var start_h=parseInt(start_time.split(":")[0]);
						if(start_min==0){
							var min_len=0;
						}else{
							var min_len=(start_min/60)*colWidth;
						}
						if(start_h<=8){
							start_h=8;
						}
						var start_left=Math.round(oRoomId.clientWidth)+Math.round(start_h-8)*colWidth+min_len;//+10
						var end_time=data[i]["BookList"][j]["End"].split(" ")[1];
						var end_min=parseInt(end_time.split(":")[1]);
						var end_h=parseInt(end_time.split(":")[0]);
						if(end_h==0 || end_h>=18){
							end_h=18;
						}
						if(end_min==0){
							var end_len=0;
						}else{
							var end_len=(end_min/60)*colWidth;
						}
						var end_left=Math.round(oRoomId.clientWidth)+(end_h-8)*colWidth+end_len;
						var oMax=document.querySelectorAll(".meetRoom")[0];
						if(end_left>=oMax.clientWidth){
							end_left=oMax.clientWidth;
						}
						if(start_left<=Math.round(oRoomId.clientWidth)){
							start_left=Math.round(oRoomId.clientWidth);
						}
						if(data[i]["BookList"].length>1){
							for(var m=1;m<data[i]["BookList"].length;m++){
								if(parseInt(data[i]["BookList"][j]["Start"].split(" ")[1])>parseInt(data[i]["BookList"][m]["Start"].split(" ")[1])){
									oDiv.style.zIndex=zIndexNum+2;
								}else{
									oDiv.style.zIndex=zIndexNum;
								}
							}
						}else{
							oDiv.style.zIndex=zIndexNum;
						}
						
						var w=end_left-start_left;
						var Top=i*30;
						oDiv.style.top=Top+4+"px";
						var h=22;
						oDiv.style.height=h+"px";
						oDiv.style.fontSize="14px";
						oDiv.style.lineHeight=h+"px";
						oDiv.style.left=start_left+"px";
						oDiv.style.width=w+"px";
						oDiv.style.position="absolute";
						oDiv.style.color="#fff";
						oDiv.style.overflow="hidden";
						oDiv.style.textIndent="6px";
						oDiv.innerHTML=data[i]["BookList"][j]["BookOwner"];
						oMain.appendChild(oDiv);
						zIndexNum+=1;
//						}else{
//							
//						}
					}
				}
			}
		}
		weekDay($scope);
		showPopup();
		changeBC();
	}
	function showPopup(){
		var oPopup=document.getElementById("popup");
		var oBookRoom=document.querySelectorAll(".booked");
		var oTitle=document.querySelectorAll(".title")[0];
		var oSelectBox=document.querySelectorAll("#selectBox")[0];
		
		var oBookRoom=document.querySelectorAll(".booked");
		var oTitle=document.querySelectorAll(".title")[0];
		var oSelectBox=document.querySelectorAll("#selectBox")[0];
		
		var arrData=[];
		for(var i=0;i<$scope.roomData.length;i++){
			if($scope.roomData[i]["BookList"]){
				for(var j=0;j<$scope.roomData[i]["BookList"].length;j++){
					arrData.push($scope.roomData[i]["BookList"][j]);
				}
			}
		}
		oPopup.style.display="none";
		var timerId=null;
		clearTimeout(timerId);
		var roomArr=[];
		for(var m=0;m<$scope.roomData.length;m++){
			roomArr.push($scope.roomData[m]["RoomName"]);
		}
		for(var k=0,len=oBookRoom.length;k<len;k++){
			let index=k;
			oBookRoom[index].onmouseenter=function(){
				var abb=document.getElementsByTagName("body")[0].getAttribute("width");
				var topNum=($(this)[0].offsetTop-4)/30;
				timerId=setTimeout(function(){
					oPopup.style.display="block";
				},300);
				oPopup.innerHTML="";
				var oTop=this.offsetTop+this.clientHeight+oTitle.clientHeight+oSelectBox.clientHeight;
				var oleft=this.offsetLeft;
				var oMeet=document.querySelectorAll(".meetRoom")[0];
				if(80+this.offsetTop+this.clientHeight>oMeet.clientHeight){
					oTop=oMeet.clientHeight-this.clientHeight;
				}
				if(210+this.offsetLeft>oMeet.clientWidth){
					oleft=oMeet.clientWidth-210;
				}
//				oPopup.style.display="block";
				oPopup.style.top=oTop+"px";
				oPopup.style.height=60+"px";
				oPopup.style.left=oleft+"px";
				oPopup.style.padding=5+"px";
//				console.log(this.getAttribute("owner"));
				for(var i=0;i<$scope.roomData.length;i++){
					if($scope.roomData[i]["BookList"]){
						for(var j=0;j<$scope.roomData[i]["BookList"].length;j++){
//							if($scope.roomData[i]["BookList"][j]["BookOwner"]==this.innerHTML && $scope.roomData[i]["RoomName"]==roomArr[topNum] && $scope.roomData[i]["BookList"][j]["BookOwner"]!=="Unknown"){
//							console.log($scope.roomData[i]["BookList"][j]["End"]);
								if($scope.roomData[i]["BookList"][j]["BookOwner"]==this.getAttribute("owner") && $scope.roomData[i]["BookList"][j]["Start"]==this.getAttribute("start") && $scope.roomData[i]["BookList"][j]["End"]==this.getAttribute("end") && $scope.roomData[i]["RoomName"]==this.getAttribute("roomid") && $scope.roomData[i]["BookList"][j]["Telephone"]==this.getAttribute("tele")){
									
									$scope.tele=$scope.roomData[i]["BookList"][j]["Telephone"];
									$scope.owner=$scope.roomData[i]["BookList"][j]["BookOwner"];
									$scope.appointment=$scope.roomData[i]["BookList"][j]["Start"].split(" ")[1]+"—"+$scope.roomData[i]["BookList"][j]["End"].split(" ")[1];
									oPopup.innerHTML='<p class="owner" ng-bind="owner"><span>Booked by：</span>'+$scope.owner+'</p><p class="time" ng-bind="appointment"><span>Book Time：</span>'+$scope.appointment+'</p><p class="tele" ng-bind="tele"><span>Contact：</span>'+$scope.tele+'</p>';
									return;
									
									
//								}
								
								
//								$scope.tele=$scope.roomData[i]["BookList"][j]["Telephone"];
//								$scope.owner=$scope.roomData[i]["BookList"][j]["BookOwner"];
//								$scope.appointment=$scope.roomData[i]["BookList"][j]["Start"].split(" ")[1]+"—"+$scope.roomData[i]["BookList"][j]["End"].split(" ")[1];
//								oPopup.innerHTML='<h4>Book information</h4><p class="owner" ng-bind="owner"><span>Book Person：</span>'+$scope.owner+'</p><p class="time" ng-bind="appointment"><span>Book Time：</span>'+$scope.appointment+'</p><p class="tele" ng-bind="tele"><span>Contact：</span>'+$scope.tele+'</p>';
//								return;
							}else if($scope.roomData[i]["BookList"][j]["BookOwner"]=="Unknown"){
								$scope.tele=arrData[index].Telephone;;
								$scope.owner=arrData[index].BookOwner;
								$scope.appointment=arrData[index].Start.split(" ")[1]+"—"+arrData[index].End.split(" ")[1];
								oPopup.innerHTML='<p class="owner" ng-bind="owner">Booked by：'+$scope.owner+'</p><p class="time" ng-bind="appointment">Book Time：'+$scope.appointment+'</p><p class="tele" ng-bind="tele">Contact：'+$scope.tele+'</p>';
								return;
							}
						}
					}
				}
			}
			oBookRoom[index].onmouseleave=function(){
				clearTimeout(timerId);
				oPopup.style.display="none";
			}
		}
	}
	
	function hash(API_URL){
		var cou;
		var cit;
		var oCountry=document.getElementById("country");
		var oCity=document.getElementById("city");
		var oItemBox=document.querySelectorAll(".itemBox");
		if(location.search){
			var oCountry=document.getElementById("country");
			var oCity=document.getElementById("city");
			oCountry.setAttribute("disabled","disabled");
			oCity.setAttribute("disabled","disabled");
			var oLoading=document.getElementById("loading");
			oLoading.style.display="block";
			var oSelect=document.querySelectorAll("select");
			var str1=location.search.substr(1).split("&");
			for(var i=0;i<str1.length;i++){
				var str2=str1[i].split("=");
				for(var j=0;j<str2.length;j++){
					if(str2[0]=="location"){
						var cit=str2[1];
					}
					if(str2[0]=="country"){
						var cou=str2[1];
					}
				}
			}
			getRoomInfo(cou,cit,today);
			clearInterval(timerInit);
			clearInterval(selectAreatimer);
			clearInterval(selectDateTimer);
			for(var i=0;i<oItemBox.length;i++){
				oItemBox[i].setAttribute("disabled","disabled");
			}
			timerInit=setInterval(function(){
				for(var i=0;i<oItemBox.length;i++){
					oItemBox[i].setAttribute("disabled","disabled");
				}
				oCountry.setAttribute("disabled","disabled");
				oCity.setAttribute("disabled","disabled");
				oLoading.style.display="block";
				getRoomInfo(cou,cit,today);
			},60000);
		}else{
			oCountry.removeAttribute("disabled","disabled");
			oCity.removeAttribute("disabled","disabled");
			for(var i=0;i<oItemBox.length;i++){
				oItemBox[i].removeAttribute("disabled","disabled");
			}
		}
	}
	function changeBC(){
		var odd=document.querySelectorAll("div.booked:nth-child(odd)");
		var even=document.querySelectorAll("div.booked:nth-child(even)");
		for(var i=0,len=odd.length;i<len;i++){
			odd[i].style.background="rgb(255,125,2)";
		}
		for(var i=0,len=even.length;i<len;i++){
			even[i].style.background="rgb(0,112,192)";
		}
	}
}])





